package com.webproject.core.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.JWTUtil;
import com.webproject.core.base.BaseException;
import com.webproject.core.base.BaseResponse;
import com.webproject.core.model.User;
import com.webproject.core.service.AuthService;
import com.webproject.core.utils.TokenUtils;
import com.webproject.core.vo.RegisterAndLoginVO;
import com.webproject.core.vo.request.LoginRequestBody;
import com.webproject.core.vo.request.RegisterRequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 身份验证controller
 * 用于校验身份信息 提供登录注册等身份相关功能
 */

@RequestMapping("/api/auth")
@RestController
public class AuthController {

    @Autowired
    private AuthService authService;

    @Value("${jwt.key}")
    private String jwtKey;

    @GetMapping("/test")
    public String testConnect(){
        // 仅做服务连通性测试 无其他作用 可删
        return "OK333333333";
    }

    /**
     * 注册流程
     */
    @PostMapping("/register")
    public BaseResponse<RegisterAndLoginVO> register(HttpServletRequest request, @RequestBody RegisterRequestBody requestBody) throws BaseException {
        if (StrUtil.isEmpty(requestBody.getUsername()) || StrUtil.isEmpty(requestBody.getUsername()) || StrUtil.isEmpty(requestBody.getUsername())){
            // 校验参数非空
            throw new BaseException("参数不能为空");
        }
        // 注册成功
        User user = authService.registerUser(requestBody.getUsername(), requestBody.getPassword());

        // 注册成功之后需要生成一个包含身份信息的jwt (json web token)
        Map<String, Object> jwtMap = new HashMap<String,Object>(){{
            put("userId",user.getId());
            put("username",user.getUsername());
        }};
        String token = JWTUtil.createToken(jwtMap, jwtKey.getBytes());

        // 返回数据，data内存放生成的jwt
        RegisterAndLoginVO resVo = new RegisterAndLoginVO();
        resVo.setToken(token);
        resVo.setUserId(user.getId());
        resVo.setUsername(user.getUsername());
        return BaseResponse.success(resVo);
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public BaseResponse<RegisterAndLoginVO> login(HttpServletRequest request, @RequestBody LoginRequestBody requestBody) throws BaseException {
        if (StrUtil.isEmpty(requestBody.getUsername()) || StrUtil.isEmpty(requestBody.getUsername())){
            // 校验参数非空
            throw new BaseException("账号或密码不能为空");
        }
        // 登录流程
        User login = authService.login(requestBody.getUsername(), requestBody.getPassword());

        // 根据登录信息 生成jwt token
        String token = TokenUtils.createJwtToken(login.getId(),login.getUsername(), jwtKey); // 调用自己封装的工具类

        // 返回数据，data内存放生成的jwt
        RegisterAndLoginVO resVo = new RegisterAndLoginVO();
        resVo.setToken(token);
        resVo.setUserId(login.getId());
        resVo.setUsername(login.getUsername());
        return BaseResponse.success(resVo);
    }


}
